package domain;

import java.util.List;

import bo.BusinessFigure;

/**
 * this clas calculate the m-Value of the equation y=m*x + q
 * @author Nikolai Straessle
 * @author Marco Serpi
 *
 */
public class Equation {
	
	private List<BusinessFigure> aList;
	private double xValues[];
	private double revenueValues[];
	private double profitValues[];
	private double xAverage = 3;
	private double revenueAverage;
	private double profitAverage;
	
	/**
	 * Constructor
	 * @param list
	 * 		java.util.List<BusinessFigure>
	 */
	public Equation(List<BusinessFigure> list) {
		this.aList = list;
		
		xValues = new double[aList.size()];
		revenueValues = new double[aList.size()];
		profitValues = new double[aList.size()];
		
		
		int i = 0;
		
		for (BusinessFigure businessFigure : this.aList) {
			xValues[i] = businessFigure.getYear();
			revenueValues[i] = businessFigure.getaRevenue();
			profitValues[i] = businessFigure.getaProfit();
			i++;
		}
		for(int k = 0; k < xValues.length; k ++){
			revenueAverage = revenueAverage + revenueValues[k];
			profitAverage = profitAverage + profitValues[k];
		}
		revenueAverage = revenueAverage / xValues.length;
		profitAverage = profitAverage / xValues.length;
	}
	
	/**
	 * get the Reveue m-Value
	 * @return
	 * 		m-Value
	 * 		x-Average
	 * 		revenue-Average
	 */
	public double[] getRevenueEquation(){
		double retVal[] = new double[3];
		retVal[0] = ((xValues[0] - xAverage)*(revenueValues[0] - revenueAverage) + (xValues[1] - xAverage)*(revenueValues[1] - revenueAverage) + (xValues[2] - xAverage)*(revenueValues[2] - revenueAverage)) / 
				((xValues[0] - xAverage)*(xValues[0] - xAverage) + (xValues[1] - xAverage)*(xValues[1] - xAverage) + (xValues[2] - xAverage)*(xValues[2] - xAverage));
		retVal[1] = xAverage;
		retVal[2] = revenueAverage;
		return retVal;
	}
	
	/**
	 * get the profit m-Value
	 * @return
	 * 		m-Value
	 * 		x-Average
	 * 		profit-Average
	 */
	public double[] getProfitEquation(){
		double retVal[] = new double[3];
		retVal[0] = ((xValues[0] - xAverage)*(profitValues[0] - profitAverage) + (xValues[1] - xAverage)*(profitValues[1] - profitAverage) + (xValues[2] - xAverage)*(profitValues[2] - profitAverage)) / 
				((xValues[0] - xAverage)*(xValues[0] - xAverage) + (xValues[1] - xAverage)*(xValues[1] - xAverage) + (xValues[2] - xAverage)*(xValues[2] - xAverage));
		retVal[1] = xAverage;
		retVal[2] = profitAverage;
		return retVal;
	}
}
